package com.hfuu.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hfuu.dao.read.custom.TbFreeScheduleReadDao;
import com.hfuu.dao.read.custom.TbUserOrginzerReadDao;
import com.hfuu.dao.read.custom.TbUserReadDao;
import com.hfuu.dao.read.custom.TbUserScheduleReadDao;
import com.hfuu.dao.write.custom.TbFreeScheduleWriteDao;
import com.hfuu.dao.write.custom.TbUserScheduleWriteDao;
import com.hfuu.enums.system.ExceptionEnum;
import com.hfuu.exceptions.ServiceExceptionSpec;
import com.hfuu.model.po.*;
import com.hfuu.service.schedule.ScheduleService;
import com.hfuu.service.schedule.request.UpdateUserScheduleRequest;
import com.hfuu.service.schedule.request.UserScheduleRequest;
import com.hfuu.service.schedule.response.UserScheduleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author: admin
 * Date: 2018/4/17
 * time: 下午5:25
 */
@Service
public class ScheduleServiceImpl extends CommonImpl implements ScheduleService {

    @Autowired
    private TbUserScheduleReadDao tbUserScheduleReadDao;

    @Autowired
    private TbUserScheduleWriteDao tbUserScheduleWriteDao;

    @Autowired
    private TbFreeScheduleReadDao tbFreeScheduleReadDao;

    @Autowired
    private TbFreeScheduleWriteDao tbFreeScheduleWriteDao;

    @Autowired
    private TbUserOrginzerReadDao tbUserOrginzerReadDao;

    @Autowired
    private TbUserReadDao tbUserReadDao;

    @Value("${lastTermStartTime}")
    private String lastTermStartTime;

    @Value("${nextTermStartTime}")
    private String nextTermStartTime;
    /**
     * 查询一个用户一个星期的课程表
     * @param request
     * @return
     * @throws ServiceExceptionSpec
     * @throws IOException
     */
    @Override
    public List<UserScheduleResponse> getUserScheduleById(UserScheduleRequest request) throws Exception {

        if (request.getUserId() == null
               || request.getWeekCount() == null) {
            throw new ServiceExceptionSpec(ExceptionEnum.MANAGE_SEARCH_ERROR);
        }

        Integer term = judgeTerm();
        //查询空闲课程表
        TbUserScheduleExample example = new TbUserScheduleExample();
        TbUserScheduleExample.Criteria criteria = example.createCriteria();
        criteria.andDisabledEqualTo(0).andWeekCountEqualTo(request.getWeekCount())
                .andUserIdEqualTo(request.getUserId()).andTermEqualTo(term);
        List<TbUserSchedule> lists = tbUserScheduleReadDao.selectByExample(example);

        if (CollectionUtils.isEmpty(lists)) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_SCHELDUE_ERROR);
        }
        TbUserSchedule tbUserSchedule = lists.get(0);

        if (StringUtils.isEmpty(tbUserSchedule.getMonday()) ||
                StringUtils.isEmpty(tbUserSchedule.getTuesday()) ||
                StringUtils.isEmpty(tbUserSchedule.getMonday()) ||
                StringUtils.isEmpty(tbUserSchedule.getThursday())
                ||StringUtils.isEmpty(tbUserSchedule.getFriday()) ||
                StringUtils.isEmpty(tbUserSchedule.getSaturday()) ||
                StringUtils.isEmpty(tbUserSchedule.getSunday())) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_SCHELDUE_ERROR);
        }

        //数据封装
        List<UserScheduleResponse> rList = new ArrayList<>();
        rList.add(objectMapper.
                readValue(tbUserSchedule.getMonday(), UserScheduleResponse.class));
        rList.add(objectMapper.
                readValue(tbUserSchedule.getTuesday(), UserScheduleResponse.class));
        rList.add(objectMapper.
                readValue(tbUserSchedule.getWednesday(), UserScheduleResponse.class));
        rList.add(objectMapper.
                readValue(tbUserSchedule.getThursday(), UserScheduleResponse.class));
        rList.add(objectMapper.
                readValue(tbUserSchedule.getFriday(), UserScheduleResponse.class));
        rList.add(objectMapper.
                readValue(tbUserSchedule.getSaturday(), UserScheduleResponse.class));
        rList.add(objectMapper.
                readValue(tbUserSchedule.getSunday(), UserScheduleResponse.class));

        return rList;
    }

    /**
     * 空闲时间表，个性化修改
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    public Integer updateUserScheduleById(UpdateUserScheduleRequest request) throws Exception {

        if (StringUtils.isEmpty(request.getType()) ||
                request.getUserId() == null ||
                request.getWeekCount() == null) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_BASE_PARAMS_ILLEGAL);
        }

        //查询出该用户该周课程表
        UserScheduleRequest userScheduleRequest = new UserScheduleRequest();
        userScheduleRequest.setUserId(request.getUserId());
        userScheduleRequest.setWeekCount(request.getWeekCount());
        List<UserScheduleResponse> lists = getUserScheduleById(userScheduleRequest);
        if (CollectionUtils.isEmpty(lists) ||
                lists.size() != 7) {
            return 0;
        }


        //进行赋值更新
        if ("am1".equals(request.getType())) {
            lists.get(0).setAm1(request.getMonday());
            lists.get(1).setAm1(request.getTuesday());
            lists.get(2).setAm1(request.getWednesday());
            lists.get(3).setAm1(request.getThursday());
            lists.get(4).setAm1(request.getFriday());
            lists.get(5).setAm1(request.getSaturday());
            lists.get(6).setAm1(request.getSunday());
        } else if ("am2".equals(request.getType())) {
            lists.get(0).setAm2(request.getMonday());
            lists.get(1).setAm2(request.getTuesday());
            lists.get(2).setAm2(request.getWednesday());
            lists.get(3).setAm2(request.getThursday());
            lists.get(4).setAm2(request.getFriday());
            lists.get(5).setAm2(request.getSaturday());
            lists.get(6).setAm2(request.getSunday());
        } else if ("am3".equals(request.getType())) {
            lists.get(0).setAm3(request.getMonday());
            lists.get(1).setAm3(request.getTuesday());
            lists.get(2).setAm3(request.getWednesday());
            lists.get(3).setAm3(request.getThursday());
            lists.get(4).setAm3(request.getFriday());
            lists.get(5).setAm3(request.getSaturday());
            lists.get(6).setAm3(request.getSunday());
        } else if ("am4".equals(request.getType())) {
            lists.get(0).setAm4(request.getMonday());
            lists.get(1).setAm4(request.getTuesday());
            lists.get(2).setAm4(request.getWednesday());
            lists.get(3).setAm4(request.getThursday());
            lists.get(4).setAm4(request.getFriday());
            lists.get(5).setAm4(request.getSaturday());
            lists.get(6).setAm4(request.getSunday());
        } else if ("noon".equals(request.getType())) {
            lists.get(0).setNoon(request.getMonday());
            lists.get(1).setNoon(request.getTuesday());
            lists.get(2).setNoon(request.getWednesday());
            lists.get(3).setNoon(request.getThursday());
            lists.get(4).setNoon(request.getFriday());
            lists.get(5).setNoon(request.getSaturday());
            lists.get(6).setNoon(request.getSunday());
        } else if ("pm1".equals(request.getType())) {
            lists.get(0).setPm1(request.getMonday());
            lists.get(1).setPm1(request.getTuesday());
            lists.get(2).setPm1(request.getWednesday());
            lists.get(3).setPm1(request.getThursday());
            lists.get(4).setPm1(request.getFriday());
            lists.get(5).setPm1(request.getSaturday());
            lists.get(6).setPm1(request.getSunday());
        } else if ("pm2".equals(request.getType())) {
            lists.get(0).setPm2(request.getMonday());
            lists.get(1).setPm2(request.getTuesday());
            lists.get(2).setPm2(request.getWednesday());
            lists.get(3).setPm2(request.getThursday());
            lists.get(4).setPm2(request.getFriday());
            lists.get(5).setPm2(request.getSaturday());
            lists.get(6).setPm2(request.getSunday());
        } else if ("pm3".equals(request.getType())) {
            lists.get(0).setPm3(request.getMonday());
            lists.get(1).setPm3(request.getTuesday());
            lists.get(2).setPm3(request.getWednesday());
            lists.get(3).setPm3(request.getThursday());
            lists.get(4).setPm3(request.getFriday());
            lists.get(5).setPm3(request.getSaturday());
            lists.get(6).setPm3(request.getSunday());
        } else if ("pm4".equals(request.getType())) {
            lists.get(0).setPm4(request.getMonday());
            lists.get(1).setPm4(request.getTuesday());
            lists.get(2).setPm4(request.getWednesday());
            lists.get(3).setPm4(request.getThursday());
            lists.get(4).setPm4(request.getFriday());
            lists.get(5).setPm4(request.getSaturday());
            lists.get(6).setPm4(request.getSunday());
        } else if ("night".equals(request.getType())) {
            lists.get(0).setNight(request.getMonday());
            lists.get(1).setNight(request.getTuesday());
            lists.get(2).setNight(request.getWednesday());
            lists.get(3).setNight(request.getThursday());
            lists.get(4).setNight(request.getFriday());
            lists.get(5).setNight(request.getSaturday());
            lists.get(6).setNight(request.getSunday());
        }

        //更新空闲时间表数据
        TbUserSchedule tbUserSchedule = new TbUserSchedule();
        tbUserSchedule.setMonday(objectMapper.writeValueAsString(lists.get(0)));
        tbUserSchedule.setTuesday(objectMapper.writeValueAsString(lists.get(1)));
        tbUserSchedule.setWednesday(objectMapper.writeValueAsString(lists.get(2)));
        tbUserSchedule.setThursday(objectMapper.writeValueAsString(lists.get(3)));
        tbUserSchedule.setFriday(objectMapper.writeValueAsString(lists.get(4)));
        tbUserSchedule.setSaturday(objectMapper.writeValueAsString(lists.get(5)));
        tbUserSchedule.setSunday(objectMapper.writeValueAsString(lists.get(6)));
        TbUserScheduleExample example = new TbUserScheduleExample();
        TbUserScheduleExample.Criteria criteria = example.createCriteria();
        criteria.andWeekCountEqualTo(request.getWeekCount());
        criteria.andTermEqualTo(judgeTerm());
        criteria.andUserIdEqualTo(request.getUserId());

        tbUserScheduleWriteDao.updateByExampleSelective(tbUserSchedule, example);

        //同步空闲用户表
        UserScheduleRequest scheduleRequest = new UserScheduleRequest();
        scheduleRequest.setUserId(request.getUserId());
        this.synchronizeScheduleById(scheduleRequest);
        return 0;

    }

    /**
     * 以用户为单位同步个性时间表，到空闲用户表中
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    public Integer synchronizeScheduleById(UserScheduleRequest request) throws Exception {

        if (request.getUserId() == null) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_BASE_PARAMS_ILLEGAL);
        }
        //获取用户名称
        TbUser tbUser = tbUserReadDao.selectByPrimaryKey(request.getUserId());

        //先查出用户所参加的组织
        TbUserOrginzerExample example = new TbUserOrginzerExample();
        TbUserOrginzerExample.Criteria criteria = example.createCriteria();
        criteria.andDisabledEqualTo(0).andUserIdEqualTo(request.getUserId());
        List<TbUserOrginzer> userOrginzers = tbUserOrginzerReadDao.selectByExample(example);
        if (CollectionUtils.isEmpty(userOrginzers)) {
            return 0;
        }
        for (TbUserOrginzer orginzer : userOrginzers) {

            TbUserScheduleExample example1 = new TbUserScheduleExample();
            TbUserScheduleExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andDisabledEqualTo(0).andUserIdEqualTo(request.getUserId());
            List<TbUserSchedule> tbUserSchedules = tbUserScheduleReadDao.selectByExample(example1);
            if (CollectionUtils.isEmpty(tbUserSchedules)) {
                return 0;
            }
            //分析是否有课
            for (TbUserSchedule tbUserSchedule : tbUserSchedules) {
                TbFreeScheduleExample example2 = new TbFreeScheduleExample();
                TbFreeScheduleExample.Criteria criteria2 = example2.createCriteria();
                criteria2.andDisabledEqualTo(0).andOrginzerEqualTo(orginzer.getOrginzerId())
                        .andWeekCountEqualTo(tbUserSchedule.getWeekCount())
                        .andTermEqualTo(tbUserSchedule.getTerm());
                List<TbFreeSchedule> tbFreeSchedules = tbFreeScheduleReadDao.selectByExample(example2);

                TbFreeSchedule result;
                boolean flag = false;
                if (CollectionUtils.isEmpty(tbFreeSchedules)) {
                    result = new TbFreeSchedule();
                    flag = true;
                } else {
                    result = tbFreeSchedules.get(0);
                }
                UserScheduleResponse monday =
                        objectMapper.readValue(tbUserSchedule.getMonday(), UserScheduleResponse.class);
                UserScheduleResponse mondayUser = flag ? new UserScheduleResponse() :
                        objectMapper.readValue(result.getMonday(), UserScheduleResponse.class);
                result.setMonday(getTbFreeSchedule(monday, mondayUser, tbUser.getUserName()));

                UserScheduleResponse tuesday =
                        objectMapper.readValue(tbUserSchedule.getTuesday(), UserScheduleResponse.class);
                UserScheduleResponse tuesdayUser = flag ? new UserScheduleResponse() :
                        objectMapper.readValue(result.getTuesday(), UserScheduleResponse.class);
                result.setTuesday(getTbFreeSchedule(tuesday, tuesdayUser, tbUser.getUserName()));

                UserScheduleResponse wednesday =
                        objectMapper.readValue(tbUserSchedule.getWednesday(), UserScheduleResponse.class);
                UserScheduleResponse wednesdayUser = flag ? new UserScheduleResponse() :
                        objectMapper.readValue(result.getWednesday(), UserScheduleResponse.class);
                result.setWednesday(getTbFreeSchedule(wednesday, wednesdayUser, tbUser.getUserName()));

                UserScheduleResponse thursday =
                        objectMapper.readValue(tbUserSchedule.getThursday(), UserScheduleResponse.class);
                UserScheduleResponse thursdayUser = flag ? new UserScheduleResponse() :
                        objectMapper.readValue(result.getThursday(), UserScheduleResponse.class);
                result.setThursday(getTbFreeSchedule(thursday, thursdayUser, tbUser.getUserName()));

                UserScheduleResponse friday =
                        objectMapper.readValue(tbUserSchedule.getFriday(), UserScheduleResponse.class);
                UserScheduleResponse fridayUser = flag ? new UserScheduleResponse() :
                        objectMapper.readValue(result.getFriday(), UserScheduleResponse.class);
                result.setFriday(getTbFreeSchedule(friday, fridayUser, tbUser.getUserName()));

                UserScheduleResponse saturday =
                        objectMapper.readValue(tbUserSchedule.getSaturday(), UserScheduleResponse.class);
                UserScheduleResponse saturdayUser = flag ? new UserScheduleResponse() :
                        objectMapper.readValue(result.getSaturday(), UserScheduleResponse.class);
                result.setSaturday(getTbFreeSchedule(saturday, saturdayUser, tbUser.getUserName()));

                UserScheduleResponse sunday =
                        objectMapper.readValue(tbUserSchedule.getSunday(), UserScheduleResponse.class);
                UserScheduleResponse sundayUser = flag ? new UserScheduleResponse() :
                        objectMapper.readValue(result.getSunday(), UserScheduleResponse.class);
                result.setSunday(getTbFreeSchedule(sunday, sundayUser, tbUser.getUserName()));

                result.setWeekCount(tbUserSchedule.getWeekCount());
                result.setOrginzer(orginzer.getOrginzerId());
                result.setTerm(tbUserSchedule.getTerm());

                if (CollectionUtils.isEmpty(tbFreeSchedules)) {
                    tbFreeScheduleWriteDao.insertSelective(result);
                    continue;
                }
                result.setId(tbFreeSchedules.get(0).getId());
                tbFreeScheduleWriteDao.updateByPrimaryKeySelective(result);

            }
        }



        return null;
    }

    /**
     * 分装一条空闲用户课程表
     * @param everyDay
     * @param tbFreeSchedule
     */
    private String getTbFreeSchedule(UserScheduleResponse everyDay,
                                     UserScheduleResponse tbFreeSchedule,
                                     String userName) throws JsonProcessingException {

        if (StringUtils.isEmpty(everyDay.getAm1())) {
            if (StringUtils.isEmpty(tbFreeSchedule.getAm1())
            || !tbFreeSchedule.getAm1().contains(userName)) {
                tbFreeSchedule.setAm1(tbFreeSchedule.getAm1() + userName + ",");
            }
        } else {
            if (!StringUtils.isEmpty(tbFreeSchedule.getAm1())) {
                tbFreeSchedule.setAm1(tbFreeSchedule.getAm1().replaceAll(userName + ",", ""));
            }
        }

        if (StringUtils.isEmpty(everyDay.getAm2())) {
            if (StringUtils.isEmpty(tbFreeSchedule.getAm2())
            || !tbFreeSchedule.getAm2().contains(userName)) {
                tbFreeSchedule.setAm2(tbFreeSchedule.getAm2() + userName + ",");
            }
        } else {
            if (!StringUtils.isEmpty(tbFreeSchedule.getAm2())) {
                tbFreeSchedule.setAm2(tbFreeSchedule.getAm2().replaceAll(userName + ",", ""));
            }
        }

        if (StringUtils.isEmpty(everyDay.getAm3())) {
            if (StringUtils.isEmpty(tbFreeSchedule.getAm3())
            || !tbFreeSchedule.getAm3().contains(userName)) {
                tbFreeSchedule.setAm3(tbFreeSchedule.getAm3() + userName + ",");
            }
        } else {
            if (!StringUtils.isEmpty(tbFreeSchedule.getAm3())) {
                tbFreeSchedule.setAm3(tbFreeSchedule.getAm3().replaceAll(userName + ",", ""));
            }
        }

        if (StringUtils.isEmpty(everyDay.getAm4())) {
            if (StringUtils.isEmpty(tbFreeSchedule.getAm4())
            || !tbFreeSchedule.getAm4().contains(userName)) {
                tbFreeSchedule.setAm4(tbFreeSchedule.getAm4() + userName + ",");
            }
        } else {
            if (!StringUtils.isEmpty(tbFreeSchedule.getAm4())) {
                tbFreeSchedule.setAm4(tbFreeSchedule.getAm4().replaceAll(userName + ",", ""));
            }
        }

        if (StringUtils.isEmpty(everyDay.getNoon())) {
            if (StringUtils.isEmpty(tbFreeSchedule.getNoon())
            || !tbFreeSchedule.getNoon().contains(userName)) {
                tbFreeSchedule.setNoon(tbFreeSchedule.getNoon() + userName + ",");
            }
        } else {
            if (!StringUtils.isEmpty(tbFreeSchedule.getNoon())) {
                tbFreeSchedule.setNoon(tbFreeSchedule.getNoon().replaceAll(userName + ",", ""));
            }
        }

        if (StringUtils.isEmpty(everyDay.getPm1())) {
            if (StringUtils.isEmpty(tbFreeSchedule.getPm1())
            || !tbFreeSchedule.getPm1().contains(userName)) {
                tbFreeSchedule.setPm1(tbFreeSchedule.getPm1() + userName + ",");
            }
        } else {
            if (!StringUtils.isEmpty(tbFreeSchedule.getPm1())) {
                tbFreeSchedule.setPm1(tbFreeSchedule.getPm1().replaceAll(userName + ",", ""));
            }
        }

        if (StringUtils.isEmpty(everyDay.getPm2())) {
            if (StringUtils.isEmpty(tbFreeSchedule.getPm2())
            || !tbFreeSchedule.getPm2().contains(userName)) {
                tbFreeSchedule.setPm2(tbFreeSchedule.getPm2() + userName + ",");
            }
        } else {
            if (!StringUtils.isEmpty(tbFreeSchedule.getPm2())) {
                tbFreeSchedule.setPm2(tbFreeSchedule.getPm2().replaceAll(userName + ",", ""));
            }
        }

        if (StringUtils.isEmpty(everyDay.getPm3())) {
            if (StringUtils.isEmpty(tbFreeSchedule.getPm3())
            || !tbFreeSchedule.getPm3().contains(userName)) {
                tbFreeSchedule.setPm3(tbFreeSchedule.getPm3() + userName + ",");
            }
        } else {
            if (!StringUtils.isEmpty(tbFreeSchedule.getPm3())) {
                tbFreeSchedule.setPm3(tbFreeSchedule.getPm3().replaceAll(userName + ",", ""));
            }
        }

        if (StringUtils.isEmpty(everyDay.getPm4())) {
            if (StringUtils.isEmpty(tbFreeSchedule.getPm4())
            || !tbFreeSchedule.getPm4().contains(userName)) {
                tbFreeSchedule.setPm4(tbFreeSchedule.getPm4() + userName + ",");
            }
        } else {
            if (!StringUtils.isEmpty(tbFreeSchedule.getPm4())) {
                tbFreeSchedule.setPm4(tbFreeSchedule.getPm4().replaceAll(userName + ",", ""));
            }
        }

        if (StringUtils.isEmpty(everyDay.getNight())) {
            if (StringUtils.isEmpty(tbFreeSchedule.getNight())
            || !tbFreeSchedule.getNight().contains(userName)) {
                tbFreeSchedule.setNight(tbFreeSchedule.getNight() + userName + ",");
            }
        } else {
            if (!StringUtils.isEmpty(tbFreeSchedule.getNight())) {
                tbFreeSchedule.setNight(tbFreeSchedule.getNight().replaceAll(userName + ",", ""));
            }
        }

        return objectMapper.writeValueAsString(tbFreeSchedule);
    }


    /**
     * 判断是上学期还是下学期
     * @return
     * @throws Exception
     */
    private Integer judgeTerm() throws Exception {

        Integer term = 0;
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date lastTerm = sf.parse(lastTermStartTime);
        Date nextTerm = sf.parse(nextTermStartTime);
        Date now = new Date();
        if (now.compareTo(lastTerm) > 0 && now.compareTo(nextTerm) < 0) {
            term = 1;
        } else if (now.compareTo(nextTerm) > 0){
            term = 2;
        }
        return term;
    }

    /**
     * 用户不在该组织，直接删除
     * @param
     * @param tbFreeSchedule
     */
    private String getDeleteTbFreeSchedule(UserScheduleResponse tbFreeSchedule,
                                     String userName) throws JsonProcessingException {

        if (!StringUtils.isEmpty(tbFreeSchedule.getAm1())) {
            tbFreeSchedule.setAm1(tbFreeSchedule.getAm1().replaceAll(userName + ",", ""));
        }

        if (!StringUtils.isEmpty(tbFreeSchedule.getAm2())) {
            tbFreeSchedule.setAm2(tbFreeSchedule.getAm2().replaceAll(userName + ",", ""));
        }

        if (!StringUtils.isEmpty(tbFreeSchedule.getAm3())) {
            tbFreeSchedule.setAm3(tbFreeSchedule.getAm3().replaceAll(userName + ",", ""));
        }
        if (!StringUtils.isEmpty(tbFreeSchedule.getAm4())) {
            tbFreeSchedule.setAm4(tbFreeSchedule.getAm4().replaceAll(userName + ",", ""));
        }

        if (!StringUtils.isEmpty(tbFreeSchedule.getPm1())) {
            tbFreeSchedule.setPm1(tbFreeSchedule.getPm1().replaceAll(userName + ",", ""));
        }

        if (!StringUtils.isEmpty(tbFreeSchedule.getPm2())) {
            tbFreeSchedule.setPm2(tbFreeSchedule.getPm2().replaceAll(userName + ",", ""));
        }
        if (!StringUtils.isEmpty(tbFreeSchedule.getPm3())) {
            tbFreeSchedule.setPm3(tbFreeSchedule.getPm3().replaceAll(userName + ",", ""));
        }
        if (!StringUtils.isEmpty(tbFreeSchedule.getPm4())) {
            tbFreeSchedule.setPm4(tbFreeSchedule.getPm4().replaceAll(userName + ",", ""));
        }
        if (!StringUtils.isEmpty(tbFreeSchedule.getNoon())) {
            tbFreeSchedule.setNoon(tbFreeSchedule.getNoon().replaceAll(userName + ",", ""));
        }
        if (!StringUtils.isEmpty(tbFreeSchedule.getNight())) {
            tbFreeSchedule.setNight(tbFreeSchedule.getNight().replaceAll(userName + ",", ""));
        }
        return objectMapper.writeValueAsString(tbFreeSchedule);
    }


    /**
     * 通过组织Id,删除一个用户在空闲用户中数据
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    public Integer deleteScheduleById(UserScheduleRequest request) throws Exception {

        //获取用户名称
        TbUser tbUser = tbUserReadDao.selectByPrimaryKey(request.getUserId());

        TbFreeScheduleExample example2 = new TbFreeScheduleExample();
        TbFreeScheduleExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andDisabledEqualTo(0).andOrginzerEqualTo(request.getOrginzerId());
        List<TbFreeSchedule> tbFreeSchedules = tbFreeScheduleReadDao.selectByExample(example2);
        if (CollectionUtils.isEmpty(tbFreeSchedules)) {
            return 0;
        }

        for (TbFreeSchedule result : tbFreeSchedules) {
            UserScheduleResponse mondayUser =
                    objectMapper.readValue(result.getMonday(), UserScheduleResponse.class);
            result.setMonday(getDeleteTbFreeSchedule(mondayUser, tbUser.getUserName()));

            UserScheduleResponse tuesdayUser =
                    objectMapper.readValue(result.getTuesday(), UserScheduleResponse.class);
            result.setTuesday(getDeleteTbFreeSchedule(tuesdayUser, tbUser.getUserName()));

            UserScheduleResponse wednesdayUser =
                    objectMapper.readValue(result.getWednesday(), UserScheduleResponse.class);
            result.setWednesday(getDeleteTbFreeSchedule(wednesdayUser, tbUser.getUserName()));

            UserScheduleResponse thursdayUser =
                    objectMapper.readValue(result.getThursday(), UserScheduleResponse.class);
            result.setThursday(getDeleteTbFreeSchedule(thursdayUser, tbUser.getUserName()));

            UserScheduleResponse fridayUser =
                    objectMapper.readValue(result.getFriday(), UserScheduleResponse.class);
            result.setFriday(getDeleteTbFreeSchedule(fridayUser, tbUser.getUserName()));

            UserScheduleResponse saturdayUser =
                    objectMapper.readValue(result.getSaturday(), UserScheduleResponse.class);
            result.setSaturday(getDeleteTbFreeSchedule(saturdayUser, tbUser.getUserName()));

            UserScheduleResponse sundayUser =
                    objectMapper.readValue(result.getSunday(), UserScheduleResponse.class);
            result.setSunday(getDeleteTbFreeSchedule(sundayUser, tbUser.getUserName()));

            tbFreeScheduleWriteDao.updateByPrimaryKeySelective(result);

        }
        return 0;
    }

    /**
     * 通过第几周和组织ID，获得空闲用户表
     * @param request
     * @return
     */
    @Override
    public List<UserScheduleResponse> getUserFreeScheduleById(UserScheduleRequest request)
            throws Exception{

        if (request.getOrginzerId() == null
                || request.getWeekCount() == null) {
            throw new ServiceExceptionSpec(ExceptionEnum.MANAGE_SEARCH_ERROR);
        }

        Integer term = judgeTerm();
        //查询空闲用户表
        TbFreeScheduleExample example = new TbFreeScheduleExample();
        TbFreeScheduleExample.Criteria criteria = example.createCriteria();
        criteria.andDisabledEqualTo(0).andWeekCountEqualTo(request.getWeekCount())
                .andOrginzerEqualTo(request.getOrginzerId()).andTermEqualTo(term);
        List<TbFreeSchedule> lists = tbFreeScheduleReadDao.selectByExample(example);

        if (CollectionUtils.isEmpty(lists)) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_FREE_SCHELDUE_ERROR);
        }
        TbFreeSchedule tbUserSchedule = lists.get(0);

        if (StringUtils.isEmpty(tbUserSchedule.getMonday()) ||
                StringUtils.isEmpty(tbUserSchedule.getTuesday()) ||
                StringUtils.isEmpty(tbUserSchedule.getMonday()) ||
                StringUtils.isEmpty(tbUserSchedule.getThursday())
                ||StringUtils.isEmpty(tbUserSchedule.getFriday()) ||
                StringUtils.isEmpty(tbUserSchedule.getSaturday()) ||
                StringUtils.isEmpty(tbUserSchedule.getSunday())) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_SCHELDUE_ERROR);
        }

        //数据封装
        List<UserScheduleResponse> rList = new ArrayList<>();
        rList.add(objectMapper.
                readValue(tbUserSchedule.getMonday(), UserScheduleResponse.class));
        rList.add(objectMapper.
                readValue(tbUserSchedule.getTuesday(), UserScheduleResponse.class));
        rList.add(objectMapper.
                readValue(tbUserSchedule.getWednesday(), UserScheduleResponse.class));
        rList.add(objectMapper.
                readValue(tbUserSchedule.getThursday(), UserScheduleResponse.class));
        rList.add(objectMapper.
                readValue(tbUserSchedule.getFriday(), UserScheduleResponse.class));
        rList.add(objectMapper.
                readValue(tbUserSchedule.getSaturday(), UserScheduleResponse.class));
        rList.add(objectMapper.
                readValue(tbUserSchedule.getSunday(), UserScheduleResponse.class));

        return rList;
    }

}
